package com.sqsxblog.hybrid.jsinterface;


import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebView;

import com.sqsxblog.hybrid.WebViewBaseActivity;

public class JsinterfaceActivity extends WebViewBaseActivity {

    private WebView mWebView;

    private JavaObject javaObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWebView = getWebView();
        javaObject = new JavaObject(this);
        mWebView.addJavascriptInterface(javaObject,"sqsx");
        mWebView.loadUrl("file:///android_asset/javascriptInterface.html");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                runJs("test","参数");
            }
        },2000);
    }

    private void runJs(String method,String params)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
        {
            mWebView.loadUrl("javascript:"+method+"(true,'"+params+"')");
        }
        else
        {
            mWebView.evaluateJavascript("javascript:"+method+"(false,'"+params+"')", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.i("return",value);
                }
            });
        }
    }
}
